package com.buka.recipe.security.strategy;

import com.alibaba.fastjson2.JSON;
import com.buka.recipe.common.result.Result;
import com.buka.recipe.common.result.ResultCode;
import com.buka.recipe.common.result.ResultGenerator;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;

/**
 * @author admin
 */
public class JsonSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
        HttpServletResponse response = event.getResponse();
        Result<?> result = ResultGenerator.genFailResult(ResultCode.UNAUTHORIZED.code(), "账号在其它地方登录, 请重新登录.");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
    }

}
